using System;
using System.Collections;
using System.DirectoryServices;
using Landscape.Business.Common;

namespace Landscape.Business.Interface.LDAP
{
	public sealed class DirectoryUser
	{
		private DirectoryEntry user;

		public string Department
		{
			get
			{
				if (this.user == null)
				{
					throw new LandscapeException("There is no active user to retrive the user details.");
				}
				string dept = string.Empty;
				foreach (object v in this.user.Properties["department"])
				{
					dept = string.Concat(dept, v as string, ", ");
				}
				return dept;
			}
		}

		public string DisplayName
		{
			get
			{
				if (this.user == null)
				{
                    throw new LandscapeException("There is no active user to retrive the user details.");
				}
				if (this.user.Properties["displayName"].Count <= 0)
				{
					return "-";
				}
				return this.user.Properties["displayName"][0] as string;
			}
		}

		public string EMail
		{
			get
			{
				if (this.user == null)
				{
                    throw new LandscapeException("There is no active user to retrive the user details.");
				}
				if (this.user.Properties["mail"].Count <= 0)
				{
					return "-";
				}
				return this.user.Properties["mail"][0] as string;
			}
		}

		public string FirstName
		{
			get
			{
				if (this.user == null)
				{
                    throw new LandscapeException("There is no active user to retrive the user details.");
				}
				if (this.user.Properties["givenName"].Count <= 0)
				{
					return "-";
				}
				return this.user.Properties["givenName"][0] as string;
			}
		}

		public string FullName
		{
			get
			{
				if (this.user == null)
				{
                    throw new LandscapeException("There is no active user to retrive the user details.");
				}
				string name = string.Empty;
				if (this.user.Properties["givenName"].Count > 0)
				{
					name = this.user.Properties["givenName"][0] as string;
				}
				if (this.user.Properties["initials"].Count > 0)
				{
					name = string.Concat(name, " ", this.user.Properties["initials"][0]);
				}
				if (this.user.Properties["sn"].Count > 0)
				{
					name = string.Concat(name, " ", this.user.Properties["sn"][0]);
				}
				return name;
			}
		}

		public Guid GUID
		{
			get
			{
				if (this.user == null)
				{
                    throw new LandscapeException("There is no active user to retrive the user details.");
				}
				return this.user.Guid;
			}
		}

		public string LastName
		{
			get
			{
				if (this.user == null)
				{
                    throw new LandscapeException("There is no active user to retrive the user details.");
				}
				if (this.user.Properties["sn"].Count <= 0)
				{
					return "-";
				}
				return this.user.Properties["sn"][0] as string;
			}
		}

		public string LoginName
		{
			get
			{
				if (this.user == null)
				{
                    throw new LandscapeException("There is no active user to retrive the user details.");
				}
				if (this.user.Properties["sAMAccountName"].Count <= 0)
				{
					return "-";
				}
				return this.user.Properties["sAMAccountName"][0] as string;
			}
		}

		public string MiddleName
		{
			get
			{
				if (this.user == null)
				{
                    throw new LandscapeException("There is no active user to retrive the user details.");
				}
				if (this.user.Properties["initials"].Count <= 0)
				{
					return "-";
				}
				return this.user.Properties["initials"][0] as string;
			}
		}

		public DirectoryUser(DirectoryEntry user)
		{
			this.user = user;
		}
	}
}